package com.xmg.boot.demo.bootcommon.utils.page;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author: ma
 * @date: 2023-12-04
 * @since: 封装MybatisPlus通用查询类
 */
@Data
public class PageQuery<T>  {
    private static final long serialVersionUID = 1L;

    /**
     * 分页的条件
     */
    private Page<T> page;
    /**
     * 当前页数
     */
    private int currPage = 1;
    /**
     * 每页条数
     */
    private int limit = 10;

    private QueryWrapper<T> queryWrapper;

    private BaseMapper baseMapper;


    public PageQuery(PagingOptions pagingOptions, BaseMapper baseMapper) {
        this.baseMapper = baseMapper;
        // 分页的参数
        this.page = new Page<>(pagingOptions.getPageIndex(), pagingOptions.getPageSize());

        //将每个类的mapper进行赋值
        this.queryWrapper = new QueryWrapper<T>();

        //组装wrapper的条件
        if (pagingOptions.getFilters() != null) {
            for (PagingFilter filter : pagingOptions.getFilters()) {

                if (StringUtils.isEmpty(filter.getField()) || StringUtils.isEmpty(filter.getOp())) {
                    continue;
                }

                String strOp = filter.getOp().trim();
                String field = filter.getField().trim();
                String term = filter.getValue();

                switch (strOp) {
                    case "eq":
                        this.queryWrapper.eq(field, term);
                        break;
                    case "ne":
                        this.queryWrapper.ne(field, term);
                        break;
                    case "lt":
                        this.queryWrapper.lt(field, term);
                        break;
                    case "le":
                        this.queryWrapper.le(field, term);
                        break;
                    case "gt":
                        this.queryWrapper.gt(field, term);
                        break;
                    case "ge":
                        this.queryWrapper.ge(field, term);
                        break;
                    case "cn":
                        this.queryWrapper.like(field, term);
                        break;
                    case "nu":
                        this.queryWrapper.isNull(field);
                        break;
                    case "nn":
                        this.queryWrapper.isNotNull(field);
                        break;
                    case "in":
                        List<String> result = Arrays.asList(term.split(","));
                        this.queryWrapper.in(field, result);
                        break;
                }

            }
        }

        if (pagingOptions.getSorts() != null) {

            for (PagingSort sort : pagingOptions.getSorts()) {

                if (StringUtils.isEmpty(sort.getField()) || StringUtils.isEmpty(sort.getSort())) {
                    continue;
                }
                String strOp = sort.getSort().trim();

                switch (strOp) {
                    case "asc":
                        this.queryWrapper.orderByAsc(sort.getField());
                        break;
                    case "desc":
                        this.queryWrapper.orderByDesc(sort.getField());
                        break;
                }
            }
        }
    }

}


